package cn.itrip.auth.controller;

import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dtos.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/api")
public class UserController {
    @Resource
    private UserService userService;

    //手机注册
    @RequestMapping(value = "/registerbyphone", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto doRegisterByPhone(@RequestBody ItripUserVO vo){
        //1.手机号验证
        if (!this.validPhone(vo.getUserCode())){
            return DtoUtil.returnFail("请使用正确的手机号码", ErrorCode.AUTH_ILLEGAL_USERCODE);
        }

        //2.调用service
        ItripUser user = new ItripUser();
        user.setUserCode(vo.getUserCode());
        user.setUserName(vo.getUserName());
        try {
            if(null!=userService.findByUserCode(user.getUserCode())) {
                return DtoUtil.returnFail("用户已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }else{
                user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
                userService.itriptxCreateUserByPhone(user);
                return DtoUtil.returnSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_UNKNOWN);
        }
    }

    /**
     * 验证是否合法的手机号
     * @param phone
     * @return
     */
    private boolean validPhone(String phone) {
        String regex="^1[3578]{1}\\d{9}$";
        return Pattern.compile(regex).matcher(phone).find();
    }

    //手机短信码验证
    @RequestMapping(value = "/validatephone", method = RequestMethod.PUT, produces = "application/json")
    @ResponseBody
    public Dto validatePhone(@RequestParam String phoneNum, @RequestParam String code){
        try {
            if(userService.validatePhone(phoneNum,code))
                return DtoUtil.returnSuccess("验证成功");
            else
                return DtoUtil.returnFail("验证失败",ErrorCode.AUTH_AUTHENTICATION_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("验证失败",ErrorCode.AUTH_UNKNOWN);
        }
    }

    //邮箱注册
    @RequestMapping(value = "/doregister", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto doRegisterByEmail(@RequestBody ItripUserVO vo) {
        //1.邮箱验证
        if(!this.validateEmail(vo.getUserCode())){
            return DtoUtil.returnFail("请使用正确的邮箱地址", ErrorCode.AUTH_ILLEGAL_USERCODE);
        }
        ItripUser user = new ItripUser();
        user.setUserCode(vo.getUserCode());
        user.setUserName(vo.getUserName());
        //user.setUserPassword(vo.getUserPassword());

        try {
            if(null!=userService.findByUserCode(vo.getUserCode())){
                return DtoUtil.returnFail("用户已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }else{
                //2.调用业务createUser
                user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
                userService.itriptxCreateUser(user);
                return DtoUtil.returnSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_UNKNOWN);
        }
        //return DtoUtil.returnFail("注册失败",ErrorCode.AUTH_UNKNOWN);
    }

    @RequestMapping(value = "/activate", method = RequestMethod.PUT, produces = "application/json")
    @ResponseBody
    public Dto activate(@RequestParam String user,@RequestParam String code){
        try {
            if(userService.activate(user,code)){
                return DtoUtil.returnSuccess("激活成功");
            }else{
                return DtoUtil.returnSuccess("激活失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("激活失败",ErrorCode.AUTH_UNKNOWN);
        }
    }


    private boolean validateEmail(String email){
        String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        return Pattern.compile(regex).matcher(email).find();
    }
}
